import {ThunkAction} from 'redux-thunk'
import {RootState} from "../store/store";
import axios from 'axios'
export const REQUEST_RECOMMEND_SUCCESS="REQUEST_RECOMMEND_SUCCESS"//success
export const REQUEST_RECOMMEND_LOADING="REQUEST_RECOMMEND_LOADING"//loading
export const REQUEST_RECOMMEND_FAIL="REQUEST_RECOMMEND_FAIL"//fail
interface successactiontype{
    type:typeof REQUEST_RECOMMEND_SUCCESS,
    payload:any
}
interface loadingactiontype{
    type:typeof REQUEST_RECOMMEND_LOADING,
    payload:any
}
interface failactiontype{
    type:typeof REQUEST_RECOMMEND_FAIL,
    payload:string|null
}
export type recommendActionType=successactiontype|loadingactiontype|failactiontype
export const successActionCreator=(productList:any[]):successactiontype=>{
    return{
        type:'REQUEST_RECOMMEND_SUCCESS',
        payload:productList
    }
}
export const loadingActionCreator=()=>{
    return{
        type:'REQUEST_RECOMMEND_LOADING',
    }
}
export const failActionCreator=(error:string):failactiontype=>{
    return{
        type:'REQUEST_RECOMMEND_FAIL',
        payload:error
    }
}
export const getrecommenddata=():ThunkAction<void ,RootState,unknown,recommendActionType>=>async (dispatch,getstate)=>{
    try{
        const {data}=await axios.get('http://123.56.149.216:8080/api/productCollections',
            {
                headers:{
                    "x-icode":"69ACF3816BF5D32D"
                }
            }
        );
        dispatch(successActionCreator(data))
    }catch (error){
        dispatch(failActionCreator(error))
    }

}